﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VNWIKI_WEB.DAO
{
    public class AccountDAO
    {
        public User Login(string name, string pass)
        {
            User user = null;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                user = (from u in dt.Users
                        where u.AccountName.Equals(name) && u.Password.Equals(pass) && u.Deleted == false
                        select u).FirstOrDefault();
            }
            catch { }
            return user;
        }

        public List<User> GetListUser()
        {
            //List<User> userList;
           VNWIKIDataContext dt = new VNWIKIDataContext();
           // userList = (from u in dt.Users
            //            orderby u.ID descending
           //             select u).ToList();
           // return userList;
            return dt.Users.ToList();
        }

        public User GetUser(string name, string email)
        {
            User user = null;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                user = (from u in dt.Users
                        where u.Name.Equals(name) && u.Email.Equals(email) && u.Deleted == true
                        select u).FirstOrDefault();
            }
            catch { }
            return user;
        }

        public User GetUserByID(int ID)
        {
            User user = null;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                user = (from u in dt.Users
                        where u.ID.Equals(ID)
                        select u).FirstOrDefault();
            }
            catch { }
            return user;
        }

        public int Add(User user) 
        {
            int rs = 0;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                dt.Users.InsertOnSubmit(user);
                dt.SubmitChanges();
                rs = 1;
            }
            catch { }
            return rs;
        }

        public int Update(User user)
        {
            int rs = 0;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                
                dt.SubmitChanges();
                rs = 1;
            }
            catch { }
            return rs;
        }

        public int ActiveAccount(int userID, string name)
        {
            int rs;
            User user = null;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                user = (from u in dt.Users
                        where u.ID.Equals(userID) && u.Name.Equals(name)
                        select u).FirstOrDefault();
                user.Deleted = false;
                dt.SubmitChanges();
                rs = 1;
            }
            catch { rs = 0; }
            return rs;
        }

        public int SignUp(string name, string accName, string password, string email)
        {
            int rs = 0;
            User user = new User();
            VNWIKIDataContext dt = new VNWIKIDataContext();
            try
            {
                user.Name = name;
                user.AccountName = accName;
                user.Password = password;
                user.Email = email;
                user.CreateDate = String.Format("{0:dd/MM/yyyy HH:mm:ss tt}", DateTime.Now);
                user.Deleted = true;
                user.Permision = false;
                user.Admin = false;
                dt.Users.InsertOnSubmit(user);
                dt.SubmitChanges();
                rs = 1;
            }
            catch { }
            return rs;
        }
    }
}